package org.wotou.authority.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;


/**
 * ecache缓存工具
 *
 * @author zhangQ
 * @date 2017/12/19
 */
public class EhcacheUtil {
    private static CacheManager cacheManager = CacheManager.create();

/*    @PostConstruct
    public void init() {
        EhcacheUtil.manager = this.ehcacheManager.getCacheManager();
    }*/

    /**
     * 缓存存放数据
     *
     * @param cacheName
     * @param key
     * @param value
     */
    public static void put(String cacheName, String key, Object value) {
        Cache cache = cacheManager.getCache(cacheName);
        Element element = cache.get(key);
        cache.put(element);
    }

    /**
     * .
     * 缓存得到数据
     *
     * @param cacheName
     * @param key
     * @return
     */
    public static Object get(String cacheName, String key) {
        Cache cache = cacheManager.getCache(cacheName);
        Element element = cache.get(key);
        return null == element ? null : element.getObjectValue();
    }

    /**
     * 得到缓存
     *
     * @param cacheName
     * @return
     */
    public static Cache get(String cacheName) {
        return cacheManager.getCache(cacheName);
    }

    /**
     * 从缓存中移除key对应的缓存
     *
     * @param cacheName
     * @param key
     */
    public static void evict(String cacheName, String key) {
        Cache cache = cacheManager.getCache(cacheName);
        cache.remove(key);
    }
}
